package com.cyj.dream.admin.persistence.manager.systems;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cyj.dream.admin.contacts.enums.IsDeleteEnum;
import com.cyj.dream.admin.model.system.SysRole;
import com.cyj.dream.admin.model.system.SysUserRole;
import com.cyj.dream.admin.persistence.service.ITbSysRoleService;
import com.cyj.dream.admin.persistence.service.ITbSysUserRoleService;
import com.cyj.dream.admin.util.IsEmptyUtils;
import com.cyj.dream.core.constant.enums.ResultCode;
import com.cyj.dream.core.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Description: 用户角色关系关联关系管理器类
 * @BelongsProject: dream-DreamChardonnay
 * @BelongsPackage: com.dream.DreamChardonnay.persistence.manager.systems
 * @Author: ChenYongJia
 * @CreateTime: 2021-05-17 09:31
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@Component
public class SysUserRoleManager {

    @Autowired
    private ITbSysUserRoleService iTbSysUserRoleService;

    @Autowired
    private ITbSysRoleService iTbSysRoleService;

    /**
     * 获取所有用户信息
     *
     * @return java.util.List<SysRole>
     * @date 2021/6/9 21:01
     * @author ChenYongJia
     * @version 1.0
     */
    public List<SysRole> getRoleLists() {
        log.info("进入 SysUserRoleManager 获取所有用户信息管理器");
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getSysRoleStatus, IsDeleteEnum.DIS_DELETE.getCode());
        wrapper.orderByDesc(true, SysRole::getSysRoleWeight);
        return iTbSysRoleService.list(wrapper);
    }

    /**
     * 查询用户关联关系表
     *
     * @param sysUserRole 用户关联关系对象
     * @return java.util.List<com.dream.DreamChardonnay.model.system.SysUserRole>
     * @date 2021/5/17 9:55
     * @author ChenYongJia
     * @version 1.0
     */
    public List<SysUserRole> selectSysUserRoleList(SysUserRole sysUserRole) {
        log.info("进入 SysUserRoleManager 查询用户关联关系表管理器，参数:{}", JSONObject.toJSONString(sysUserRole));
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        if (!IsEmptyUtils.isEmpty(sysUserRole.getSysUserId())) {
            wrapper.eq(SysUserRole::getSysUserId, sysUserRole.getSysUserId());
        }
        if (!IsEmptyUtils.isEmpty(sysUserRole.getSysRoleId())) {
            wrapper.eq(SysUserRole::getSysRoleId, sysUserRole.getSysRoleId());
        }

        return iTbSysUserRoleService.list(wrapper);
    }

    /**
     * 为用户设置角色
     *
     * @param sysUserId 用户id
     * @param sysRoleId 角色id集合
     * @return boolean
     * @date 2021/5/17 9:33
     * @author ChenYongJia
     * @version 1.0
     */
    public boolean settingSysUserRole(Long sysUserId, Set<Long> sysRoleId) {
        log.info("进入 SysUserRoleManager 为用户设置角色管理器，用户id：{}，角色id集合:{}", sysUserId, sysRoleId);
        if (!IsEmptyUtils.isEmpty(sysUserId) && !IsEmptyUtils.isEmpty(sysRoleId)) {
            List<SysUserRole> sysUserRoleList = new ArrayList<>();
            sysRoleId.stream().forEach(item -> {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setSysRoleId(item);
                sysUserRole.setSysUserId(sysUserId);
                sysUserRoleList.add(sysUserRole);
            });
            try {
                return iTbSysUserRoleService.saveBatch(sysUserRoleList);
            } catch (Exception e) {
                log.error("为用户设置角色方法调用批量添加失败，错误信息为：{}", e);
                return false;
            }
        } else {
            log.error("出现错误：方法执行失败失败！参数为空，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }
    }

    /**
     * 为用户移除设置的角色
     *
     * @param sysUserId 用户id
     * @param sysRoleId 角色id集合
     * @return boolean
     * @date 2021/5/17 9:33
     * @author ChenYongJia
     * @version 1.0
     */
    public boolean deleteSysUserRole(Long sysUserId, Set<Long> sysRoleId) {
        log.info("进入 SysUserRoleManager 为用户移除设置的角色管理器，用户id：{}，角色id集合:{}", sysUserId, sysRoleId);
        if (!IsEmptyUtils.isEmpty(sysUserId) && !IsEmptyUtils.isEmpty(sysRoleId)) {
            sysRoleId.stream().forEach(item -> {
                LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(SysUserRole::getSysUserId, sysUserId);
                wrapper.eq(SysUserRole::getSysRoleId, item);
                try {
                    iTbSysUserRoleService.remove(wrapper);
                } catch (Exception e) {
                    log.error("为用户移除设置的角色方法调用删除失败，错误信息为：{}", e);
                }
            });
            return true;
        } else {
            log.error("出现错误：方法执行失败失败！参数为空，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }
    }

}
